package 线程池;

import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-09-27
 * Time: 14:25
 */

class MyThreadPool{
    //用来管理n个任务
    private BlockingQueue<Runnable> queue = new LinkedBlockingDeque();
   public void submit(Runnable runnable) throws InterruptedException {
    queue.put(runnable);
   }
   //m个线程的线程池
   public MyThreadPool(int m){
       for (int i = 0; i < m; i++) {
           Thread t = new Thread(){
               @Override
               public void run() {
                   while (true){//线程池里的线程持续扫描阻塞队列里的任务
                       try {
                           queue.take().run();
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
               }
           };
           t.start();
       }

   }
}
public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool pool = new MyThreadPool(10);
        for (int i = 0; i < 1000; i++) {
            int thId = i;
            //创建一个任务
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("当前任务:"+thId+"线程名"+Thread.currentThread().getName());
                }
            });
        }

    }

}
